package com.yc.bigdata.hbase.support;

import com.google.common.collect.Lists;
import com.yc.bigdata.hbase.core.HBaseTemplate;

import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * <p></p>
 *
 * @author: YuanChilde
 * @date: 2020-01-16 16:58
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-01-16 16:58    YuanChilde     1.0        新增
 */

public class InitTestData {

    private static final Logger LOGGER = LoggerFactory.getLogger(InitTestData.class);
    private final HBaseTemplate hBaseTemplate;
    private final UserRepository userRepository;
    private String tableName = "users";

    public InitTestData(HBaseTemplate hBaseTemplate, UserRepository userRepository) {
        this.hBaseTemplate = hBaseTemplate;
        this.userRepository = userRepository;
        try {
            this.hBaseTemplate.deleteTable(tableName);
            this.hBaseTemplate.createTable(tableName, Lists.newArrayList(Bytes.toString(UserRepository.CF_INFO)));
        } catch (IOException e) {
            LOGGER.error("启动初始化demo数据出错");
        }
    }

    public void addUsers() {
        for (int i = 0; i < 10; i++) {
            userRepository.save("user" + i, "user" + i + "@yahoo.com", "password" + i);
        }
    }

}
